/** 
 * @file yfrobot
 * @brief YFROBOT's sensors Mind+ library.
 * @n This is a MindPlus graphics programming extension for YFROBOT's module.
 * 
 * @copyright    YFROBOT,2022
 * @copyright    MIT Lesser General Public License
 * 
 * @author [email](yfrobot@qq.com)
 * @date  2022-05-12
*/

enum LFSENSORSNUM {
    //% blockId="LFS_1" block="1"
    0,
    //% blockId="LFS_2" block="2"
    1,
    //% blockId="LFS_3" block="3"
    2,
    //% blockId="LFS_4" block="4"
    3,
    //% blockId="LFS_5" block="5"
    4
}

//% color="#ff6f0c" iconWidth=40 iconHeight=40
namespace qtr5rc {

    //% block="QTR-5RC initialize [IR1] [IR2] [IR3] [IR4] [IR5], Emitter Pin [EmitterPin]" blockType="command"
    //% IR1.shadow="dropdown" IR1.options="PIN_DigitalRead"
    //% IR2.shadow="dropdown" IR2.options="PIN_DigitalRead"
    //% IR3.shadow="dropdown" IR3.options="PIN_DigitalRead"
    //% IR4.shadow="dropdown" IR4.options="PIN_DigitalRead"
    //% IR5.shadow="dropdown" IR5.options="PIN_DigitalRead"
    //% EmitterPin.shadow="dropdown" EmitterPin.options="PIN_DigitalRead"
    export function init(parameter: any, block: any) {
        let ir1 = parameter.IR1.code;
        let ir2 = parameter.IR2.code;
        let ir3 = parameter.IR3.code;
        let ir4 = parameter.IR4.code;
        let ir5 = parameter.IR5.code;
        let emitter = parameter.EmitterPin.code;

        Generator.addInclude(`QTRSensors`,`#include "QTRSensors.h"`);
        Generator.addObject(`QTRSensorsObject`, `QTRSensors`, `qtr`);
        Generator.addSetup(`qtr.setTypeRC`, `qtr.setTypeRC();`);
        Generator.addSetup(`qtr.setSensorPins`, `qtr.setSensorPins((const uint8_t[]){${ir1},${ir2},${ir3},${ir4},${ir5}},5);`);
        Generator.addSetup(`qtr.setEmitterPin`, `qtr.setEmitterPin(${emitter});`);
        
    }

    //% block="QTR-5RC Calibrate" blockType="command"
    export function calibrate(parameter: any, block: any) {
        Generator.addCode(`qtr.calibrate();`);
    }

    //% block="QTR-5RC Read Sensor [SENSORVALUE]" blockType="reporter"
    //% SENSORVALUE.shadow="string"   SENSORVALUE.defl="sensorValues"
    export function readLineBlack(parameter: any, block: any) {
        let sensorvalue = parameter.SENSORVALUE.code;
        sensorvalue = sensorvalue.replace(/"/g, ""); //去除字符串引号
        Generator.addInclude(`define_qtr${sensorvalue}`, `uint16_t ${sensorvalue}[5];`);
        Generator.addCode(`qtr.readLineBlack(${sensorvalue})`);
    }
    
    //% block="QTR-5RC Read Sensor [SENSORVALUE] on White" blockType="reporter"
    //% SENSORVALUE.shadow="string"   SENSORVALUE.defl="sensorValues"
    export function readLineWhite(parameter: any, block: any) {
        let sensorvalue = parameter.SENSORVALUE.code;
        sensorvalue = sensorvalue.replace(/"/g, ""); //去除字符串引号
        Generator.addInclude(`define_qtr${sensorvalue}`, `uint16_t ${sensorvalue}[5];`);
        Generator.addCode(`qtr.readLineWhite(${sensorvalue})`);
    }

    //% block="[SENSORVALUE][SVALUE]" blockType="reporter"
    //% SENSORVALUE.shadow="string"   SENSORVALUE.defl="sensorValues"
    //% SVALUE.shadow="dropdown"   SVALUE.options="LFSENSORSNUM"     SD.defl="LFSENSORSNUM.0"
    export function sensorsValue(parameter: any, block: any) {
        let sensorvalue = parameter.SENSORVALUE.code;
        let sValue = parameter.SVALUE.code;
        sensorvalue = sensorvalue.replace(/"/g, ""); //去除字符串引号
        Generator.addCode(`${sensorvalue}[${sValue}]`);
    }
}
